<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\BaoxiaoModel;
use app\admin\model\ExpensesModel;
use app\admin\model\IncomeModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\Menu;

class MainController extends AdminBaseController
{

    /**
     *  后台欢迎页
     */
    public function index()
    {
        $time = date('Y-m');

        //报销
        $count_bx = BaoxiaoModel::where('apply_time','gt',$time)->count();
        $money_bx = BaoxiaoModel::where('apply_time','gt',$time)->sum('money');
        $this->assign('count_bx',$count_bx);
        $this->assign('money_bx',$money_bx);
        //支出
        $count_expenses = ExpensesModel::where('apply_time','gt',$time)->count();
        $money_expenses = ExpensesModel::where('apply_time','gt',$time)->sum('money');
        $this->assign('count_expenses',$count_expenses);
        $this->assign('money_expenses',$money_expenses);

        //收入
        $count_income = IncomeModel::where('time','gt',$time)->count();
        $money_income = IncomeModel::where('time','gt',$time)->sum('money');
        $this->assign('count_income',$count_income);
        $this->assign('money_income',$money_income);

        return $this->fetch();
    }

    public function dashboardWidget()
    {
        $dashboardWidgets = [];
        $widgets          = $this->request->param('widgets/a');
        if (!empty($widgets)) {
            foreach ($widgets as $widget) {
                if ($widget['is_system']) {
                    array_push($dashboardWidgets, ['name' => $widget['name'], 'is_system' => 1]);
                } else {
                    array_push($dashboardWidgets, ['name' => $widget['name'], 'is_system' => 0]);
                }
            }
        }

        cmf_set_option('admin_dashboard_widgets', $dashboardWidgets, true);

        $this->success('更新成功!');

    }

    //统计数据
    public function statistics()
    {
        $time = strtotime( date('Y-m',strtotime('-1 year')));
        $wcst = Db::query("select left(time,7) as month, count(id) as count
from sh_income where type=1 and time>={$time}
group by left(time,7)");
        $wcst_amount = Db::query("select left(time,7) as month, sum(if(money is null,0,money)) as sum
from sh_income where type=1 and time>={$time}
group by left(time,7)");


        $wtx = Db::query("select left(time,7) as month, count(id) as count
from sh_income where type=2 and time>={$time}
group by left(time,7)");
        $wtx_amount = Db::query("select left(time,7) as month, sum(if(money is null,0,money)) as sum
from sh_income where type=2 and time>={$time}
group by left(time,7)");



        $income = Db::query("select left(time,7) as month, count(id) as count
from sh_income where time>={$time}
group by left(time,7)");

        $income_amount = Db::query("select left(time,7) as month, sum(if(money is null,0,money)) as sum
from sh_income where time>={$time}
group by left(time,7)");


        $count['wcst']  = count($wcst);
        $count['wtx']   = count($wtx);
        $max            = max($count);
        $flip_count     = array_flip($count);
        $key            = $flip_count[$max];
        $month          = [];
        $data_wcst      = [];
        $data_wcst_amount       = [];
        $data_wtx               = [];
        $data_wtx_amount        = [];
        $data_income            = [];
        $data_income_amount     = [];

        foreach ($wcst as $ky=>$vy){
            $data_wcst[$vy['month']] = $vy['count'];
        }

        foreach ($wcst_amount as $kya=>$vya){
            $data_wcst_amount[$vya['month']] = $vya['sum'];
        }

        foreach ($wtx as $kw=>$vw){
            $data_wtx[$vw['month']] = $vw['count'];
        }
        foreach ($wtx_amount as $vwa){
            $data_wtx_amount[$vwa['month']] = $vwa['sum'];
        }

        foreach ($income as $ki=>$vi){
            $data_income[$vi['month']] = $vi['count'];
        }
        foreach ($income_amount as $ki=>$via){
            $data_income_amount[$via['month']] = $via['sum'];
        }



        foreach ($$key as $k=>$v){
            $month[] = $v['month'];
            if(!array_key_exists($v['month'],$data_wcst)){
                $data_wcst[$v['month']] = 0;
            }
            if(!array_key_exists($v['month'],$data_wcst_amount)){
                $data_wcst_amount[$v['month']] = 0;
            }
            if(!array_key_exists($v['month'],$data_wtx)){
                $data_wtx[$v['month']] = 0;
            }
            if(!array_key_exists($v['month'],$data_wtx_amount)){
                $data_wtx_amount[$v['month']] = 0;
            }
            if(!array_key_exists($v['month'],$data_income_amount)){
                $data_income_amount[$v['month']] = 0;
            }

        }
        ksort($data_wcst);
        ksort($data_wtx);
        ksort($data_wcst_amount);
        ksort($data_wtx_amount);
        ksort($data_income_amount);
        ksort($data_income);
        $data_info['label'] = $month;
        $data_info['wcst']          = array_values($data_wcst);
        $data_info['wcst_amount']   = array_values($data_wcst_amount);
        $data_info['income']        = array_values($data_income);
        $data_info['income_amount'] = array_values($data_income_amount);
        $data_info['wtx']           = array_values($data_wtx);
        $data_info['wtx_amount']    = array_values($data_wtx_amount);
        return json(['code'=>0,'data'=>$data_info]);
    }

}
